//: Playground - noun: a place where people can play

import UIKit



typealias Distance = Double


struct Position {
    var x: Double
    var y: Double
}

extension Position {
    func inRange(range: Distance) -> Bool {
        return sqrt(x * x + y * y) <= range
    }
}

extension Position {
    func minus(p: Position) -> Position {
        return Position(x: x - p.x, y: y - p.y)
    }
    
    var length: Double {
        return sqrt(x * x + y * y)
    }
}

struct Ship {
    var position: Position
    var firingRange: Distance
    var unsafeRange: Distance
}

extension Ship {
    //MARK: 检测一艘船是否在攻击范围内
    func canEngageShip(target: Ship) -> Bool {
        let dx = target.position.x - position.x
        let dy = target.position.y - position.y
        let targetDistance = sqrt(dx * dx + dy * dy)
        return targetDistance <= firingRange
    }
    
    
    //MARK: 谁知判断一艘船在不安全范围:
    func canSafelyEngageShip1(target: Ship,friendly: Ship) -> Bool {
        let targetDistance = target.position.minus(position).length
        let friendlyDistance = friendly.position.minus(position).length
        return targetDistance <= firingRange && targetDistance > unsafeRange && friendlyDistance > unsafeRange
    }
}

// 还是是第一等成员

// 定义一个点是否在某一个范围中

typealias Region = (position:Position) -> Bool


func circle(radius: Distance) -> Region {
    return { point in point.length <= radius }
}

let p1 = Position(x: 2, y: 2)
let ifInRegion = circle(5)(position:
    p1)

func circle2(radius: Distance,center: Position) -> Region {
    return { point in point.minus(center).length <= radius }
}

func shift(region: Region, offet: Position) -> Region {
    return { point in region(position: point.minus(offet))}
}

shift(circle(10),offet: Position(x: 5, y: 5))

//MARK: 新区域是由原区域以外的点组成
func invert(region: Region) -> Region {
    return { point in !region(position: point) }
}

//MARK: 两个区域的交集与并集
func intersection(region1: Region,_ region2: Region) -> Region {
    return { point in region1(position: point) && region2(position: point) }
}


func union(region1: Region,_ region2: Region) -> Region {
    return { point in region1(position: point) || region2(position: point) }
}

//MARK: 只在原区域不在第二个区域的点
// 思路：先获取第二个区域的反选区域，再与第一个区域做交集
func difference(region: Region, minus: Region) -> Region {
    return intersection(region, invert(minus))
}


extension Ship {
    func canSafelyEngageShip(target: Ship, friendly: Ship) -> Bool {
        let rangeRegion = difference(circle(firingRange), minus: circle(unsafeRange))
        let firingRegion = shift(rangeRegion,offet: position)
        let firendlyRegion = shift(circle(unsafeRange), offet: friendly.position)
        let resultRegion = difference(firingRegion, minus: firendlyRegion)
        return resultRegion(position: target.position)
    }
}

typealias test = (Position,Position) -> Bool

func testFunc(radius: Distance) -> test {
    return { (point1,point2) in point1.length <= radius && point2.length >= radius }
}


func add1(a: Int, _ b: Int) -> Int {
    return a + b
}

let result1 = add1(1,1)

typealias AddNum = Int -> Int

func add2(a: Int) -> AddNum {
    return { b in a + b }
}

let result2 = add2(1)(1)

extension Int {
    func add3(b: Int) -> AddNum {
        return { _ in self + b }
    }
}





